package com.test.example.controller.captcha;

import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.response.ApiResponse;
import com.xingluo.tools.captcha.CaptchaUtils;
import com.xingluo.tools.captcha.utils.SliderCheckData;
import com.xingluo.tools.exception.CaptchaException;
import com.xingluo.tools.exception.ToolsException;
import com.xingluo.tools.result.Result;
import cloud.tianai.captcha.application.vo.CaptchaResponse;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.ThreadLocalRandom;

@Tag(name="图片、数学、短信、滑动验证码")
@RestController
public class CaptchaController {

    @Resource
    private CaptchaUtils captchaUtils;

    @Operation(summary = "发送图片验证码", description = "发送图片验证码")
    @GetMapping("/code")
    public Result code() {
        return captchaUtils.generateImageCode(4);
    }

    @Operation(summary = "发送图片验证码图片展示", description = "发送图片验证码图片展示")
    @GetMapping("/codeImg")
    public String codeImg() {
        Result result = captchaUtils.generateImageCode(4);
        return "<img src='"+result.get("img")+"'/><h3>"+result.get("uuid")+"</h3>";
    }

    @Operation(summary = "验证图片验证码", description = "验证图片验证码")
    @GetMapping("/codeValidate")
    public String codeImgValidate(String code, String uuid) {
        try{
            if (captchaUtils.validateImageCode(code,uuid))
                return "成功";
            else return "错误";
        }catch (CaptchaException e){
            return e.getMessage();
        }
    }

    @Operation(summary = "发送数学验证码", description = "发送数学验证码")
    @GetMapping("/math")
    public Result math() {
        return captchaUtils.generateMathImageCode();
    }

    @Operation(summary = "发送数学验证码图片展示", description = "发送数学验证码图片展示")
    @GetMapping("/mathImg")
    public String mathImg() {
        Result result = captchaUtils.generateMathImageCode();
        return "<img src='"+result.get("img")+"'/><h3>"+result.get("uuid")+"</h3>";
    }

    @Operation(summary = "发送短信验证码", description = "发送短信验证码")
    @GetMapping("/telCode")
    public Result tel() {
        try {
            return captchaUtils.sendSmsCode("17610103232");
        }catch (ToolsException e){
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "短信验证码验证", description = "短信验证码验证")
    @GetMapping("/telValidate")
    public String telValidate(String code, String tel) {
        try{
            if (captchaUtils.validateSmsCode(tel,code))
                return "成功";
            else return "错误";
        }catch (CaptchaException e){
            return e.getMessage();
        }
    }

    @Operation(summary = "请求滑动验证码", description = "请求滑动验证码")
    @PostMapping("/sliderCode")
    public CaptchaResponse<ImageCaptchaVO> slider(@RequestParam("type")String type) {
        if ("RANDOM".equals(type)) {
            int i = ThreadLocalRandom.current().nextInt(0, 4);
            if (i == 0) {
                type = CaptchaTypeConstant.SLIDER;
            } else if (i == 1) {
                type = CaptchaTypeConstant.CONCAT;
            } else if (i == 2) {
                type = CaptchaTypeConstant.ROTATE;
            } else{
                type = CaptchaTypeConstant.WORD_IMAGE_CLICK;
            }
        }
        return captchaUtils.generateSliderCaptcha(type);
    }

    @Operation(summary = "滑动验证码验证", description = "滑动验证码验证")
    @PostMapping("/sliderCheck")
    public ApiResponse<?> checkCaptcha(@RequestBody SliderCheckData data) {
        return captchaUtils.checkSlider(data.getId(), data.getData());
    }

    /**
     * 二次验证
     * @param id id
     * @return boolean
     */
    @Operation(summary = "滑动验证码二次验证", description = "滑动验证码二次验证")
    @GetMapping("/sliderCheck2")
    public boolean check2Captcha(@RequestParam("id") String id) {
        // 如果开启了二次验证
        return captchaUtils.validateSliderSecondary(id);
    }
}
